// pages/user/exchange.js
var app = getApp();
var server = require("../../../utils/server.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentData: 0,
    form:true,
    
    point:false,
    money:false,
  },

  //选项卡
  //获取当前滑块的index
  bindchange: function (e) { const that = this; that.setData({ currentData: e.detail.current }) },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    const that = this;
    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      that.setData({ currentData: e.target.dataset.current })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var  that = this;
    var  info = app.globalData.userInfo;
    that.setData({
      userinfo:info,
    })
  },

//积分校验
  point:function(e){
    var that = this;
    var reg =  /^\+?[1-9][0-9]*$/;
    if(!reg.test(e.detail.value)){
      wx.showToast({
        title: '请输入合法数字',
        icon:'none',
      })
      that.setData({
        point:false,
      })
    }else{
      that.setData({
        point: true,
      })
    }
  },

//余额校验
money:function(e){
  var that = this;
  var reg = /^\+?[1-9][0-9]*$/;
  if (!reg.test(e.detail.value)) {
    wx.showToast({
      title: '请输入合法数字',
      icon: 'none',
    })
    that.setData({
      money: false,
    })
  } else {
    that.setData({
      money: true,
    })
  }
},

formSubmit:function(e){
  var that = this;
  // if(that.data.)
  console.log(e)
  //充值积分
  if (that.data.currentData == 0){
    if(!that.data.money){
      wx.showToast({
        title: '请输入合法兑换金额',
        icon:"none"
      })
      return;
    }
    if(!that.data.form){
      wx.showToast({
        title: '操作过于频繁,请稍后再试',
        icon:"none"
      })
      return;
    }
    that.setData({
      form:false,
    })
    server.postJSON("/user/exchangePoints",{userid:app.globalData.userInfo.user_id,money:e.detail.value.money},function(e){
      if(e.data.code <0){
        wx.showToast({
          title: e.data.msg,
          icon:"none"
        })
        that.setData({
          form:true,
        })
        return;
      }else{
        app.globalData.userInfo = e.data.info;
        wx.showToast({
          title: e.data.msg,
          icon:"none"
        })
        that.setData({
          form:true,
        })
        setTimeout(function(){
          wx,wx.navigateBack({
            delta: 1,
          })
        },1000)
      }
    })
  }
  //兑换余额
  if (that.data.currentData == 1){
    console.log(that.data.point)
    if (!that.data.point) {
      wx.showToast({
        title: '请输入合法兑换积分',
        icon: "none"
      })
      return;
    }
    if (!that.data.form) {
      wx.showToast({
        title: '操作过于频繁,请稍后再试',
        icon: "none"
      })
      return;
    }
    that.setData({
      form: false,
    })
    server.postJSON("/user/exchangeMoney", { userid: app.globalData.userInfo.user_id, money: e.detail.value.point }, function (e) {
        if (e.data.code < 0) {
          wx.showToast({
            title: e.data.msg,
            icon: "none"
          })
          that.setData({
            form: true,
          })
          return;
        } else {
          app.globalData.userInfo = e.data.info;
          wx.showToast({
            title: e.data.msg,
            icon: "none"
          })
          that.setData({
            form: true,
          })
          setTimeout(function () {
            wx, wx.navigateBack({
              delta: 1,
            })
          }, 1000)
        }
    })
  }
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})